﻿using System;
using System.Xml.Linq;
using System.Windows.Forms;
using SedionFormDesigner.CusControls;
using SedionFormDesigner.CusControls.DataGridView;
using System.Drawing;
using SedionFormDesigner.CusControls.Infrastructure;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;

namespace SedionFormDesigner.Serializer
{
    public class SeriCtrlAndXml
    {
        private SeriCtrlAndXml() { }
        private static SeriCtrlAndXml instance = null;
        public static SeriCtrlAndXml Instance
        {
            get
            {
                if (instance == null)
                    instance = new SeriCtrlAndXml();
                return instance;
            }
        }

        #region ControlToXml

        //private static Control getForm(Control parentCtrl)
        //{
        //    if (parentCtrl.GetType() == typeof(System.Windows.Forms.Form))
        //        return parentCtrl;
        //    else
        //    {
        //        Control result = parentCtrl;
        //        if (parentCtrl.Controls.Count > 0)
        //        {
        //            foreach (Control ctrl in parentCtrl.Controls)
        //            {
        //                result = getForm(ctrl);
        //            }
        //        }
        //        return result;
        //    }
        //}

        public XElement ControlToXml(Control form, Int32 index)
        {
            var controls = form.Controls;
            var width = form.Width;
            var height = form.Height == 784 ? 790 : form.Height;
            XElement xml = new XElement("TabPage" + index);
            xml.SetAttributeValue("Width", width);
            xml.SetAttributeValue("Height", height);
            xml.SetAttributeValue("Top", height * (index - 1));
            int maxheight = 0;
            foreach (Control item in controls)
            {
                maxheight = maxheight < (item.Height + item.Top) ? (item.Height + item.Top) : maxheight;
                var type = item.GetType().Name;
                switch (type)
                {
                    case "BD_Label":
                        xml.Add(getBD_Label(item));
                        break;
                    case "BD_TextBox":
                        xml.Add(getBD_TextBox(item));
                        break;
                    case "BD_RichTextBox":
                        xml.Add(getBD_RichTextBox(item));
                        break;
                    case "BD_Button":
                        xml.Add(getBD_Button(item));
                        break;
                    case "BD_RadioButton":
                        xml.Add(getBD_RadioButton(item));
                        break;
                    case "BD_CheckBox":
                        xml.Add(getBD_CheckBox(item));
                        break;
                    case "BD_ComboBox":
                        xml.Add(getBD_ComboBox(item));
                        break;
                    case "BD_PictureBox":
                        xml.Add(getBD_PictureBox(item));
                        break;
                    case "BD_DataGridView":
                        xml.Add(getBD_DataGridView(item));
                        break;
                    case "BD_DateTimePicker":
                        xml.Add(getBD_DateTimePicker(item));
                        break;
                    case "BD_Currency":
                        xml.Add(getBD_Currency(item));
                        break;
                    case "BD_Horizon":
                        xml.Add(getBD_Horizon(item));
                        break;
                    case "BD_Vertical":
                        xml.Add(getBD_Vertical(item));
                        break;
                }
            }
            xml.SetAttributeValue("maxHeight", maxheight);
            return xml;
        }

        /// <summary>
        ///  获得控件的公共属性
        /// </summary>
        private XElement getComElement(Control item)
        {
            XElement xml = new XElement("field");
            xml.Add(new XElement("Text", item.Text));
            xml.Add(new XElement("Left", item.Left));
            xml.Add(new XElement("Top", item.Top));
            xml.Add(new XElement("Width", item.Width));
            xml.Add(new XElement("Height", item.Height));
            xml.Add(new XElement("ForeColor", item.ForeColor));
            xml.Add(new XElement("BackColor", item.BackColor));
            xml.Add(new XElement("FontName", item.Font.Name));
            xml.Add(new XElement("FontSize", item.Font.Size));
            xml.Add(new XElement("Name", item.Name));
            return xml;
        }

        private XElement getBD_Label(Control item)
        {
            XElement xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_Label).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_Label).FontItalic));
            xml.Add(new XElement("TextAlign", (item as BD_Label).TextAlign));
            xml.Add(new XElement("Type", "BD_Label"));
            return xml;
        }

        private XElement getBD_TextBox(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_TextBox).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_TextBox).FontItalic));
            xml.Add(new XElement("TextAlign", (item as BD_TextBox).TextAlign));
            xml.Add(new XElement("Limit", (item as BD_TextBox).Limit));
            xml.Add(new XElement("Limittype", (item as BD_TextBox).Limittype));
            xml.Add(new XElement("ArgName", (item as BD_TextBox).ArgName));
            xml.Add(new XElement("AutoBind", (item as BD_TextBox).AutoBind));
            xml.Add(new XElement("Formular", (item as BD_TextBox).Formular));
            xml.Add(new XElement("Type", "BD_TextBox"));

            return xml;
        }

        private XElement getBD_RichTextBox(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_RichTextBox).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_RichTextBox).FontItalic));
            xml.Add(new XElement("Limit", (item as BD_RichTextBox).Limit));
            xml.Add(new XElement("Limittype", (item as BD_RichTextBox).Limittype));
            xml.Add(new XElement("Type", "BD_RichTextBox"));

            return xml;
        }

        private XElement getBD_Button(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_Button).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_Button).FontItalic));
            xml.Add(new XElement("TextAlign", (item as BD_Button).TextAlign));
            xml.Add(new XElement("Type", "BD_Button"));

            return xml;
        }

        private XElement getBD_RadioButton(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_RadioButton).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_RadioButton).FontItalic));
            xml.Add(new XElement("Checked", (item as RadioButton).Checked));
            xml.Add(new XElement("Type", "BD_RadioButton"));

            return xml;
        }

        private XElement getBD_CheckBox(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_CheckBox).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_CheckBox).FontItalic));
            xml.Add(new XElement("Checked", (item as CheckBox).Checked));
            xml.Add(new XElement("Type", "BD_CheckBox"));

            return xml;
        }

        private XElement getBD_ComboBox(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_ComboBox).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_ComboBox).FontItalic));
            xml.Add(new XElement("AutoBind", (item as BD_ComboBox).AutoBind));
            xml.Add(new XElement("Type", "BD_ComboBox"));

            var SQL = String.Empty;
            switch ((item as BD_ComboBox).AutoBind)
            {
                case SedionFormDesigner.CusControls.ComboBox.Autobing.绑定到本部门人员:
                    SQL = "select staff_id 用户编码,staff_name 用户名称  from jc_staff  where staff_kybz='在职' and dept_id =@deptid";
                    break;

                case SedionFormDesigner.CusControls.ComboBox.Autobing.绑定到本公司职位:
                    SQL = "select duty_id 职位编码,duty_name 职位名称  from jc_duty";
                    break;
                case CusControls.ComboBox.Autobing.不绑定:
                    var ops = (item as BD_ComboBox).Items;
                    if (ops.Count > 0)
                    {
                        SQL = String.Concat(ops[0], ',', ops[0], ';');
                        for (int i = 1; i < ops.Count; i++)
                            SQL = String.Concat(SQL, ops[i], ',', ops[i], ';');
                    }
                    break;
            }

            xml.Add(new XElement("SQL", SQL));
            return xml;
        }

        private XElement getBD_PictureBox(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_PictureBox).FontBold));
            xml.Add(new XElement("FontItalic"), (item as BD_PictureBox).FontItalic);
            xml.Add(new XElement("Image", (item as BD_PictureBox).ImageLocation));
            xml.Add(new XElement("Type", "BD_PictureBox"));

            return xml;
        }

        private XElement getBD_DateTimePicker(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_DateTimePicker).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_DateTimePicker).FontItalic));
            xml.Add(new XElement("TimeValue", (item as BD_DateTimePicker).Value.ToString("yyyy-MM-dd")));
            xml.Add(new XElement("Type", "BD_DateTimePicker"));

            return xml;
        }

        private XElement getBD_Currency(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("CurrencyType", (item as BD_Currency).CurrencyType));
            xml.Add(new XElement("FontBold", (item as BD_Currency).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_Currency).FontItalic));
            xml.Add(new XElement("TextAlign", (item as BD_Currency).TextAlign));
            xml.Add(new XElement("Type", "BD_Currency"));

            return xml;
        }

        private XElement getBD_TreeView_Employee(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_TreeView_Employee).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_TreeView_Employee).FontItalic));
            xml.Add(new XElement("Type", "BD_TreeView_Employee"));

            return xml;
        }

        private XElement getBD_DataGridView(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_DataGridView).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_DataGridView).FontItalic));
            xml.Add(new XElement("Type", "BD_DataGridView"));

            var operType = (item as BD_DataGridView).OperType;
            xml.Add(new XElement("OperType", operType.ToString()));
            if (operType == CusControls.DataGridView.OprType.绑定数据库)
            {
                xml.Add(new XElement("SQL", (item as BD_DataGridView).SQL));
            }
            else
            {
                if (operType == OprType.可增表格)
                    xml.Add(new XElement("TableType", "Addable"));
                else
                    xml.Add(new XElement("TableType", "normal"));
                var colsValue = String.Empty;
                var cols = (item as BD_DataGridView).Columns;
                if (cols != null && cols.Count > 0)
                {
                    for (int i = 0; i < cols.Count; i++)
                    {
                        var column = cols[i] as ExternPropTextBoxColumn;
                        colsValue = String.Concat(colsValue, column.HeaderText, '-', column.Name, '-', column.Fillable.ToString(), '-', column.Gather, ';');
                    }
                    colsValue = colsValue.Remove(colsValue.Length - 1);
                }
                xml.Add(new XElement("Columns", colsValue));

                var rowsValue = String.Empty;
                var rows = (item as BD_DataGridView).Rows;
                if (rows != null && rows.Count > 1)
                {
                    for (int i = 0; i < rows.Count - 1; i++)
                    {
                        for (int j = 0; j < cols.Count; j++)
                        {
                            var column = cols[j] as ExternPropTextBoxColumn;
                            rowsValue = String.Concat(rowsValue, rows[i].Cells[j].Value, '-', column.Fillable.ToString(), ',');
                        }
                        rowsValue = rowsValue.Remove(rowsValue.Length - 1);
                        rowsValue = String.Concat(rowsValue, ';');
                    }
                    rowsValue = rowsValue.Remove(rowsValue.Length - 1);
                }
                xml.Add(new XElement("Rows", rowsValue));
            }

            return xml;
        }

        private XElement getBD_Horizon(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_Horizon).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_Horizon).FontItalic));
            xml.Add(new XElement("Type", "BD_Horizon"));

            return xml;
        }

        private XElement getBD_Vertical(Control item)
        {
            var xml = getComElement(item);
            xml.Add(new XElement("FontBold", (item as BD_Vertical).FontBold));
            xml.Add(new XElement("FontItalic", (item as BD_Vertical).FontItalic));
            xml.Add(new XElement("Type", "BD_Vertical"));

            return xml;
        }

        #endregion

        #region XmlToControl

        public List<Control> XmlToControl(XElement xml)
        {
            var elems = xml.Elements();
            List<Control> ctrlList = new List<Control>();
            foreach (XElement item in elems)
            {
                var typeName = item.Element("Type").Value;
                var type = Type.GetType("SedionFormDesigner.CusControls." + typeName);
                var ctrl = Activator.CreateInstance(type);
                ctrl = getBaseCtrl(item, (Control)ctrl);
                switch (typeName)
                {
                    case "BD_Label":
                        ctrlList.Add(getBD_Label(item, (BD_Label)ctrl));
                        break;
                    case "BD_TextBox":
                        ctrlList.Add(getBD_TextBox(item, (BD_TextBox)ctrl));
                        break;
                    case "BD_RichTextBox":
                        ctrlList.Add(getBD_RichTextBox(item, (BD_RichTextBox)ctrl));
                        break;
                    case "BD_Button":
                        ctrlList.Add(getBD_Button(item, (BD_Button)ctrl));
                        break;
                    case "BD_CheckBox":
                        ctrlList.Add(getBD_CheckBox(item, (BD_CheckBox)ctrl));
                        break;
                    case "BD_RadioButton":
                        ctrlList.Add(getBD_RadioButton(item, (BD_RadioButton)ctrl));
                        break;
                    case "BD_ComboBox":
                        ctrlList.Add(getBD_ComboBox(item, (BD_ComboBox)ctrl));
                        break;
                    case "BD_PictureBox":
                        ctrlList.Add(getBD_Picture(item, (BD_PictureBox)ctrl));
                        break;
                    case "BD_DateTimePicker":
                        ctrlList.Add(getBD_DateTimePicker(item, (BD_DateTimePicker)ctrl));
                        break;
                    case "BD_DataGridView":
                        ctrlList.Add(getBD_DataGridView(item, (BD_DataGridView)ctrl));
                        break;
                    case "BD_Horizon":
                        ctrlList.Add(getBD_Horizon(item, (BD_Horizon)ctrl));
                        break;
                    case "BD_Vertical":
                        ctrlList.Add(getBD_Vertical(xml, (BD_Vertical)ctrl));
                        break;
                    case "BD_Currency":
                        ctrlList.Add(getBD_Currency(item, (BD_Currency)ctrl));
                        break;
                    case "BD_TreeView_Employee":
                        ctrlList.Add(getBD_TreeView_Employee(item, (BD_TreeView_Employee)ctrl));
                        break;
                    default:
                        break;
                }
            }
            return ctrlList;
        }

        public List<Control> XmlToControl(IDesignerLoaderHost host, XElement xml)
        {
            var elems = xml.Elements();
            List<Control> ctrlList = new List<Control>();
            foreach (XElement item in elems)
            {
                var typeName = item.Element("Type").Value;
                var type = Type.GetType("SedionFormDesigner.CusControls." + typeName);
                var name = item.Element("Name").Value;
                Object ctrl = null;
                if (typeof(IComponent).IsAssignableFrom(type))
                    ctrl = host.CreateComponent(type, name);
                else
                    ctrl = Activator.CreateInstance(type);

                ctrl = getBaseCtrl(item, (Control)ctrl);





                switch (typeName)
                {
                    case "BD_Label":
                        ctrlList.Add(getBD_Label(item, (BD_Label)ctrl));
                        break;
                    case "BD_TextBox":
                        ctrlList.Add(getBD_TextBox(item, (BD_TextBox)ctrl));
                        break;
                    case "BD_RichTextBox":
                        ctrlList.Add(getBD_RichTextBox(item, (BD_RichTextBox)ctrl));
                        break;
                    case "BD_Button":
                        ctrlList.Add(getBD_Button(item, (BD_Button)ctrl));
                        break;
                    case "BD_CheckBox":
                        ctrlList.Add(getBD_CheckBox(item, (BD_CheckBox)ctrl));
                        break;
                    case "BD_RadioButton":
                        ctrlList.Add(getBD_RadioButton(item, (BD_RadioButton)ctrl));
                        break;
                    case "BD_ComboBox":
                        ctrlList.Add(getBD_ComboBox(item, (BD_ComboBox)ctrl));
                        break;
                    case "BD_PictureBox":
                        ctrlList.Add(getBD_Picture(item, (BD_PictureBox)ctrl));
                        break;
                    case "BD_DateTimePicker":
                        ctrlList.Add(getBD_DateTimePicker(item, (BD_DateTimePicker)ctrl));
                        break;
                    case "BD_DataGridView":
                        ctrlList.Add(getBD_DataGridView(item, (BD_DataGridView)ctrl));
                        break;
                    case "BD_Horizon":
                        ctrlList.Add(getBD_Horizon(item, (BD_Horizon)ctrl));
                        break;
                    case "BD_Vertical":
                        ctrlList.Add(getBD_Vertical(xml, (BD_Vertical)ctrl));
                        break;
                    case "BD_Currency":
                        ctrlList.Add(getBD_Currency(item, (BD_Currency)ctrl));
                        break;
                    case "BD_TreeView_Employee":
                        ctrlList.Add(getBD_TreeView_Employee(item, (BD_TreeView_Employee)ctrl));
                        break;
                    default:
                        break;
                }
            }
            return ctrlList;
        }

        private Control getBaseCtrl(XElement xml, Control ctrl)
        {
            ctrl.Text = xml.Element("Text").Value;
            ctrl.Name = xml.Element("Name").Value;
            ctrl.Left = Convert.ToInt32(xml.Element("Left").Value);
            ctrl.Top = Convert.ToInt32(xml.Element("Top").Value);
            ctrl.Width = Convert.ToInt32(xml.Element("Width").Value);
            ctrl.Height = Convert.ToInt32(xml.Element("Height").Value);

            return ctrl;
        }

        private BD_Label getBD_Label(XElement xml, BD_Label lable)
        {

            var fontBold = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontBold").Value) ? "false" : xml.Element("FontBold").Value);
            var fontItalic = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontItalic").Value) ? "false" : xml.Element("FontItalic").Value);
            var fontSize = float.Parse(xml.Element("FontSize").Value);
            lable.FontSize = fontSize;
            lable.FontItalic = fontItalic;
            lable.FontBold = fontBold;

            lable.NewName = xml.Element("Name").Value;
            lable.TextAlign = (TextAlign)Enum.Parse(typeof(TextAlign), xml.Element("TextAlign").Value);
            return lable;
        }

        private BD_TextBox getBD_TextBox(XElement xml, BD_TextBox textbox)
        {
            var fontBold = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontBold").Value) ? "false" : xml.Element("FontBold").Value);
            var fontItalic = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontItalic").Value) ? "false" : xml.Element("FontItalic").Value);
            var fontSize = float.Parse(xml.Element("FontSize").Value);
            textbox.FontSize = fontSize;
            textbox.FontItalic = fontItalic;
            textbox.FontBold = fontBold;

            textbox.NewName = xml.Element("Name").Value;
            textbox.TextAlign = (TextAlign)Enum.Parse(typeof(TextAlign), xml.Element("TextAlign").Value);
            textbox.Limit = xml.Element("Limit").Value;
            textbox.Limittype = (Limitype)Enum.Parse(typeof(Limitype), xml.Element("Limittype").Value);
            textbox.ArgName = xml.Element("ArgName").Value;
            textbox.Formular = xml.Element("Formular").Value;
            //自动绑定参数 的属性
            if (null != xml.Element("AutoBind"))
            {
                if (SedionFormDesigner.CusControls.TextBox.Autobing.绑定到部门.ToString() == xml.Element("AutoBind").Value)
                {
                    textbox.AutoBind = SedionFormDesigner.CusControls.TextBox.Autobing.绑定到部门;
                }
                else if (SedionFormDesigner.CusControls.TextBox.Autobing.绑定到发起人.ToString() == xml.Element("AutoBind").Value)
                {
                    textbox.AutoBind = SedionFormDesigner.CusControls.TextBox.Autobing.绑定到发起人;
                }
                else if (SedionFormDesigner.CusControls.TextBox.Autobing.绑定到日期.ToString() == xml.Element("AutoBind").Value)
                {
                    textbox.AutoBind = SedionFormDesigner.CusControls.TextBox.Autobing.绑定到日期;
                }
                else if (SedionFormDesigner.CusControls.TextBox.Autobing.绑定到编号.ToString() == xml.Element("AutoBind").Value)
                {
                    textbox.AutoBind = SedionFormDesigner.CusControls.TextBox.Autobing.绑定到编号;
                }
                else if (SedionFormDesigner.CusControls.TextBox.Autobing.绑定到职位.ToString() == xml.Element("AutoBind").Value)
                {
                    textbox.AutoBind = SedionFormDesigner.CusControls.TextBox.Autobing.绑定到职位;
                }
            }
            return textbox;
        }

        private BD_RichTextBox getBD_RichTextBox(XElement xml, BD_RichTextBox richtextbox)
        {
            var fontBold = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontBold").Value) ? "false" : xml.Element("FontBold").Value);
            var fontItalic = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontItalic").Value) ? "false" : xml.Element("FontItalic").Value);
            var fontSize = float.Parse(xml.Element("FontSize").Value);
            richtextbox.FontSize = fontSize;
            richtextbox.FontItalic = fontItalic;
            richtextbox.FontBold = fontBold;

            richtextbox.Limit = xml.Element("Limit").Value;
            richtextbox.Limittype = (Limitype)Enum.Parse(typeof(Limitype), xml.Element("Limittype").Value);
            return richtextbox;
        }

        private BD_Button getBD_Button(XElement xml, BD_Button button)
        {
            var fontBold = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontBold").Value) ? "false" : xml.Element("FontBold").Value);
            var fontItalic = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontItalic").Value) ? "false" : xml.Element("FontItalic").Value);
            var fontSize = float.Parse(xml.Element("FontSize").Value);
            button.FontSize = fontSize;
            button.FontItalic = fontItalic;
            button.FontBold = fontBold;

            button.TextAlign = (TextAlign)Enum.Parse(typeof(TextAlign), xml.Element("TextAlign").Value);
            return button;
        }

        private BD_CheckBox getBD_CheckBox(XElement xml, BD_CheckBox checkbox)
        {
            var fontBold = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontBold").Value) ? "false" : xml.Element("FontBold").Value);
            var fontItalic = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontItalic").Value) ? "false" : xml.Element("FontItalic").Value);
            var fontSize = float.Parse(xml.Element("FontSize").Value);
            checkbox.FontSize = fontSize;
            checkbox.FontItalic = fontItalic;
            checkbox.FontBold = fontBold;

            checkbox.Checked = Boolean.Parse(xml.Element("Checked").Value);
            return checkbox;
        }

        private BD_RadioButton getBD_RadioButton(XElement xml, BD_RadioButton radiobutton)
        {
            var fontBold = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontBold").Value) ? "false" : xml.Element("FontBold").Value);
            var fontItalic = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontItalic").Value) ? "false" : xml.Element("FontItalic").Value);
            var fontSize = float.Parse(xml.Element("FontSize").Value);
            radiobutton.FontSize = fontSize;
            radiobutton.FontItalic = fontItalic;
            radiobutton.FontBold = fontBold;

            radiobutton.Checked = Boolean.Parse(xml.Element("Checked").Value);
            return radiobutton;
        }

        private BD_ComboBox getBD_ComboBox(XElement xml, BD_ComboBox combobox)
        {
            var fontBold = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontBold").Value) ? "false" : xml.Element("FontBold").Value);
            var fontItalic = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontItalic").Value) ? "false" : xml.Element("FontItalic").Value);
            var fontSize = float.Parse(xml.Element("FontSize").Value);
            combobox.FontSize = fontSize;
            combobox.FontItalic = fontItalic;
            combobox.FontBold = fontBold;

            if (xml.Element("SQL") != null)
            {
                combobox.SQL = xml.Element("SQL").Value;
            }
            else
            {
                var items = xml.Element("Items").Value;
                foreach (var item in items)
                {
                    combobox.Items.Add(item);
                }
            }
            if (null != xml.Element("AutoBind"))
            {
                if (xml.Element("AutoBind").Value == SedionFormDesigner.CusControls.ComboBox.Autobing.绑定到本部门人员.ToString())
                    combobox.AutoBind = SedionFormDesigner.CusControls.ComboBox.Autobing.绑定到本部门人员;
                else if (xml.Element("AutoBind").Value == SedionFormDesigner.CusControls.ComboBox.Autobing.绑定到本公司职位.ToString())
                    combobox.AutoBind = SedionFormDesigner.CusControls.ComboBox.Autobing.绑定到本公司职位;
            }
            return combobox;
        }

        private BD_PictureBox getBD_Picture(XElement xml, BD_PictureBox picturebox)
        {
            picturebox.ImageLocation = xml.Element("Image").Value;
            return picturebox;
        }

        private BD_DateTimePicker getBD_DateTimePicker(XElement xml, BD_DateTimePicker datetimepicker)
        {
            var fontBold = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontBold").Value) ? "false" : xml.Element("FontBold").Value);
            var fontItalic = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontItalic").Value) ? "false" : xml.Element("FontItalic").Value);
            var fontSize = float.Parse(xml.Element("FontSize").Value);
            datetimepicker.FontSize = fontSize;
            datetimepicker.FontItalic = fontItalic;
            datetimepicker.FontBold = fontBold;

            datetimepicker.Value = DateTime.Parse(xml.Element("TimeValue").Value);
            return datetimepicker;
        }

        private BD_DataGridView getBD_DataGridView(XElement xml, BD_DataGridView datagridview)
        {
            var operType = xml.Element("OperType").Value;

            datagridview.OperType = (SedionFormDesigner.CusControls.DataGridView.OprType)Enum.Parse(typeof(SedionFormDesigner.CusControls.DataGridView.OprType), operType);

            if (operType == "绑定数据库")
            {
                datagridview.SQL = xml.Element("SQL").Value;
                datagridview.OperType = OprType.绑定数据库;
            }
            else
            {
                if (xml.Element("TableType").Value == "Addable")
                    datagridview.OperType = OprType.可增表格;
                else
                    datagridview.OperType = OprType.手动填写;


                var colstr = xml.Element("Columns").Value;
                if (colstr == String.Empty) return datagridview;
                var colstrs = colstr.Split(';');
                DataGridView innerGrdView = datagridview;
                foreach (var col in colstrs)
                {
                    ExternPropTextBoxColumn extCol = new ExternPropTextBoxColumn();
                    var colInfo = col.Split('-');
                    extCol.HeaderText = colInfo[0];
                    extCol.Name = colInfo[1];
                    extCol.Fillable = Boolean.Parse(colInfo[2]);
                    extCol.Gather = colInfo[3];
                    innerGrdView.Columns.Add(extCol);
                    datagridview.Columns = innerGrdView.Columns;
                }

                var rowstr = xml.Element("Rows").Value;
                var rowstrs = rowstr.Split(';');
                foreach (var row in rowstrs)
                {
                    var rows = row.Split(',');
                    var rowArry = new String[rows.Length];
                    for (int i = 0; i < rows.Length; i++)
                    {
                        var rw = rows[i].Split('-');
                        rowArry[i] = rw[0];
                    }
                    datagridview.Rows.Add(rowArry);
                }
            }
            return datagridview;
        }

        private BD_Horizon getBD_Horizon(XElement xml, BD_Horizon horizeon)
        {
            return horizeon;
        }

        private BD_Vertical getBD_Vertical(XElement xml, BD_Vertical vertical)
        {
            return vertical;
        }

        private BD_Currency getBD_Currency(XElement xml, BD_Currency currency)
        {
            var fontBold = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontBold").Value) ? "false" : xml.Element("FontBold").Value);
            var fontItalic = Boolean.Parse(string.IsNullOrEmpty(xml.Element("FontItalic").Value) ? "false" : xml.Element("FontItalic").Value);
            var fontSize = float.Parse(xml.Element("FontSize").Value);
            currency.FontSize = fontSize;
            currency.FontItalic = fontItalic;
            currency.FontBold = fontBold;

            currency.TextAlign = (TextAlign)Enum.Parse(typeof(TextAlign), xml.Element("TextAlign").Value);
            return currency;
        }

        private BD_TreeView_Employee getBD_TreeView_Employee(XElement xml, BD_TreeView_Employee treeviewemployee)
        {
            return treeviewemployee;
        }

        #endregion
    }
}
